<?php


namespace App\Utils;


use App\Contract\Platform;
use App\Model\Staff;
use App\Model\Agent;
use App\User;

class Token
{
    protected static $user = null;

    protected static string $platform = '';

    public static function verifyToken($token, $platform): bool
    {
        // platform saas总后台，agent服务商后台， mp小程序api
        switch ($platform) {
            case Platform::SAAS:
                $user = Staff::query()->where('token', $token)->first();
                break;
            case Platform::AGENT:
                $user = Agent::query()->where('token', $token)->first();
                break;
            case Platform::MP:
                $user = User::query()->where('remember_token', $token)->first();
                break;
            case Platform::BUSINESS:
                $user = User::query()->where('remember_token', $token)->first();
                break;
            default:
                $user = null;
        }
        if ($user) {
            self::$user = $user;
            self::$platform = $platform;
        }
        return (bool)$user;
    }

    public static function getUser()
    {
        return self::$user;
    }

    public static function getPlatform(): string
    {
        return self::$platform;
    }
}
